//
//  JEHOnePicTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/7.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHOnePicTableViewCell.h"

@interface JEHOnePicTableViewCell ()

@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UILabel *replyLabel;

@end

@implementation JEHOnePicTableViewCell

- (void)dealloc {
    
    [_firstImage release];
    [_titleLabel release];
    [_introLabel release];
    [_replyLabel release];
    [_rootModel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.firstImage.frame = CGRectMake(5, 5, self.width - 10, self.height - 60 - 10);
    CGFloat replyWidth = [self getWidthWithText:self.replyLabel.text withFont:10];
    self.replyLabel.frame = CGRectMake(self.width - replyWidth - 5, _firstImage.height, replyWidth, 30);
    self.titleLabel.frame = CGRectMake(5, self.height - 60, self.width - replyWidth - 10, 30);
    self.introLabel.frame = CGRectMake(5, self.height - 30, self.width - 10, 30);
}

- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}

- (void)setRootModel:(JEHRootModel *)rootModel {
    if (_rootModel != rootModel) {
        [_rootModel release];
        _rootModel = [rootModel retain];
    }
    JEHSecondModel *second = rootModel.picsModel.listArray[0];
    [self.firstImage sd_setImageWithURL:[NSURL URLWithString:second.kpic] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.titleLabel.text = rootModel.title;
    self.introLabel.text = rootModel.intro;
    self.replyLabel.text = [NSString stringWithFormat:@"%ld评论", (long)rootModel.comment];
}

- (void)createView {
    
    self.firstImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_firstImage release];
    _firstImage.contentMode = UIViewContentModeScaleAspectFit;
    _firstImage.clipsToBounds = YES;
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_titleLabel release];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    self.introLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_introLabel release];
    _introLabel.font = [UIFont systemFontOfSize:12];
    _introLabel.textColor = [UIColor grayColor];
    _introLabel.numberOfLines = 0;
    self.replyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_replyLabel release];
    _replyLabel.font = [UIFont systemFontOfSize:10];
    _replyLabel.textColor = [UIColor grayColor];
    
    [self addSubview:_firstImage];
    [self addSubview:_titleLabel];
    [self addSubview:_introLabel];
    [self addSubview:_replyLabel];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
